package com.project.decoration.controller.cases;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.decoration.common.enums.BusinessType;
import com.project.decoration.common.enums.ResultCode;
import com.project.decoration.domain.DecorationCasePrice;
import com.project.decoration.domain.Result;
import com.project.decoration.service.DecorationCasePriceService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 超级管理员
 * @version 1.0
 * @description: 案例费用明细controller
 * @date 2025/02/13 08:19
 */
@Controller
@ResponseBody
@RequestMapping("price")
public class DecorationCasePriceController {

    @Autowired
    private DecorationCasePriceService decorationCasePriceService;

    /** 分页获取案例费用明细 */
    @PostMapping("getDecorationCasePricePage")
    public Result getDecorationCasePricePage(@RequestBody DecorationCasePrice decorationCasePrice) {
        Page<DecorationCasePrice> page = new Page<>(decorationCasePrice.getPageNumber(),decorationCasePrice.getPageSize());
        QueryWrapper<DecorationCasePrice> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(StringUtils.isNotBlank(decorationCasePrice.getCaseId()),DecorationCasePrice::getCaseId,decorationCasePrice.getCaseId())
                .like(StringUtils.isNotBlank(decorationCasePrice.getName()),DecorationCasePrice::getName,decorationCasePrice.getName());
        Page<DecorationCasePrice> decorationCasePricePage = decorationCasePriceService.page(page, queryWrapper);
        return Result.success(decorationCasePricePage);
    }

    @GetMapping("getDecorationCasePriceByCaseId")
    public Result getDecorationCasePriceByCaseId(@RequestParam("caseId")String caseId) {
        QueryWrapper<DecorationCasePrice> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(DecorationCasePrice::getCaseId,caseId);
        List<DecorationCasePrice> priceList = decorationCasePriceService.list(queryWrapper);
        return Result.success(priceList);
    }

    /** 根据id获取案例费用明细 */
    @GetMapping("getDecorationCasePriceById")
    public Result getDecorationCasePriceById(@RequestParam("id")String id) {
        DecorationCasePrice decorationCasePrice = decorationCasePriceService.getById(id);
        return Result.success(decorationCasePrice);
    }

    /** 保存案例费用明细 */
    @PostMapping("saveDecorationCasePrice")
    public Result saveDecorationCasePrice(@RequestBody DecorationCasePrice decorationCasePrice) {
        boolean save = decorationCasePriceService.save(decorationCasePrice);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 编辑案例费用明细 */
    @PostMapping("editDecorationCasePrice")
    public Result editDecorationCasePrice(@RequestBody DecorationCasePrice decorationCasePrice) {
        boolean save = decorationCasePriceService.updateById(decorationCasePrice);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 删除案例费用明细 */
    @GetMapping("removeDecorationCasePrice")
    public Result removeDecorationCasePrice(@RequestParam("ids")String ids) {
        if (StringUtils.isNotBlank(ids)) {
            String[] asList = ids.split(",");
            for (String id : asList) {
                decorationCasePriceService.removeById(id);
            }
            return Result.success();
        } else {
            return Result.fail("案例费用明细id不能为空！");
        }
    }

}
